//
//  EventViewCell.m
//  lvtu
//
//  Created by Apple on 12-4-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "EventViewCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation EventViewCell
@synthesize statusButton;
@synthesize placeInfo;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        cellProfilePic = [[EGOImageView alloc] initWithFrame:CGRectMake(0, 5, 80, 70)];
        cellProfilePic.backgroundColor = [UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0f];
        cellProfilePic.layer.borderWidth = 1.0f;
		cellProfilePic.layer.borderColor = [UIColor colorWithRed:197.0/255.0 green:197.0/255.0 blue:197.0/255.0 alpha:1.0].CGColor;
        [self.contentView addSubview:cellProfilePic];
        
        cellText = [[UILabel alloc]initWithFrame:CGRectMake(85, 0, 165, 75)];
        cellText.backgroundColor = [UIColor clearColor];
        cellText.numberOfLines = 4;
        cellText.font = [UIFont systemFontOfSize:12.0f];
        [self.contentView addSubview:cellText];
        
        statusButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 35, 30, 30)];
        [statusButton setBackgroundImage:[UIImage imageNamed:@"btn_check_normal.png"] forState:UIControlStateNormal];
        [statusButton setBackgroundImage:[UIImage imageNamed:@"btn_check_press.png"] forState:UIControlStateSelected];
        [statusButton addTarget:self action:@selector(selectButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:statusButton];
    }
    return self;
}

- (void)selectButtonPressed
{
    if (isAdd) {
        if (![statusButton isSelected]) {
            [statusButton setSelected:YES];
            NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:placeInfo,@"info", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"cellclick" object:self userInfo:dict];
        }
        else {
            [statusButton setSelected:NO];
            NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:placeInfo,@"info", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"celldelclick" object:self userInfo:dict];
        }
    }
    else {
        NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:placeInfo,@"info", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"celldelclick" object:self userInfo:dict];
    }
}

- (void)setButtonType:(NSInteger) type
{
    if (type == 0) {
        [statusButton setBackgroundImage:[UIImage imageNamed:@"btn_check_normal.png"] forState:UIControlStateNormal];
        [statusButton setBackgroundImage:[UIImage imageNamed:@"btn_check_press.png"] forState:UIControlStateSelected];
        isAdd = YES;
    }
    else
    {
        [statusButton setBackgroundImage:[UIImage imageNamed:@"btn_delete_normal.png"] forState:UIControlStateNormal];
        [statusButton setBackgroundImage:[UIImage imageNamed:@"btn_delete_press.png"] forState:UIControlStateHighlighted];
        isAdd = NO;
    }
}


- (void)setInfo:(PlaceInfo*)info status:(BOOL)isSelect
{
    PlaceInfo* newPlaceInfo;
    if ([info isKindOfClass:[NSDictionary class]]) {
        NSDictionary* dict = (NSDictionary*)info;
        newPlaceInfo = [[PlaceInfo alloc] init];
        newPlaceInfo.shopid = [dict objectForKey:@"c_shop_id"];
        newPlaceInfo.name = [dict objectForKey:@"name"];
        newPlaceInfo.review = [dict objectForKey:@"review"];
        newPlaceInfo.imageurl = [dict objectForKey:@"image"];
        self.placeInfo = newPlaceInfo;
    }
    else {
        self.placeInfo = info;
    }
    [cellProfilePic setImageURL:[NSURL URLWithString:placeInfo.imageurl]];
    cellText.text = [NSString stringWithFormat:@"%@\n%@",placeInfo.name ,placeInfo.review];
    [statusButton setSelected:isSelect];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    
}

- (void)dealloc {
    [cellProfilePic release]; cellProfilePic = nil;
    [cellText release]; cellText = nil;
    
    [statusButton release]; statusButton = nil;
    [super dealloc];
}
@end
